#include <stdio.h>
#include <stdlib.h>
#include "stack.h"

/*
 * 
void CError (Stack S) {
    if(InfoTop(S) == 'z') {
        printf("Program doesn't begin correctly\n");
    } else if(InfoTop(S) == 'w') {
        printf("expected 'do' after 'while'\n");
    }
}

int main(int argc, char** argv) {
    char* input;
    int byte_read;
    int nbytes = 100;
    char * prev;
    char * baca;
    infotype a;
    int i,j,k, cek,c ,y;
    Stack S;
    CreateEmpty(&S);
    a = 'z';
    Push(&S,a);
    FILE *filegue;
    filegue = fopen("input.txt","r");
    input = (char *) malloc (nbytes + 1);
    baca = (char *) malloc (nbytes+1);
    prev = (char *) malloc (nbytes+1);
    for (j = 0; j < nbytes+1; j++) {
                    prev[j] = '\0';
        }
    do
    {
        for (j = 0; j < nbytes+1; j++) {
                    input[j] = '\0';
        }
        byte_read = fgets(input,nbytes,filegue);
        printf("%d\n",byte_read);
        if(byte_read != 0)
        {
            i = 0;
            k = 0;
            cek = 1;
            while((input[i] != '\0') && (input[i] != '\n'))
            {
                //if((input[0] = 'b')&&(input[1] = 'e')&&(input[2] = 'g')&&(input[3] = 'i')&&(input[4] = 'n'))
                for (j = 0; j < (nbytes+1) ; j++) {
                    baca[j] = '\0';
                }
                while ((input[i] == ' ') || (input[i] == '\t')) {
                    i++;
                }
                k = 0;
                while ((input[i] != ' ') && (input[i] != '\0') && (input[i] != '\n')) {
                    baca[k] = input[i];
                    k++;
                    i++;
                }
                prev = baca;
                if ((baca[0] == 'b') && (baca[1] == 'e') && (baca[2] == 'g') && (baca[3] == 'i') && (baca[4] == 'n') && (k <=5)) {
                    a = 'b';
                    Push(&S,a);
                } else if ((baca[0] == 'e') && (baca[1] == 'n') && (baca[2] == 'd') && (k <= 3)) {
                    if (InfoTop(S) == 'b') {
                        Pop(&S,&a);
                    } else {
                        CError(S);
                        cek = 0;
                    }
                } else if ((baca[0] == 'w') && (baca[1] == 'h') && (baca[2] == 'i') && (baca[3] == 'l') && (baca[4] == 'e') && (k <=5)) {
                    a = 'w';
                    Push(&S,a);
                } else if ((baca[0] == 'd') && (baca[1] == 'o') && (k<=2)) {
                    if (InfoTop(S) == 'w') {
                        Pop(&S,&a);
                    } else {
                        CError(S);
                        cek = 0;
                    }
                } else if((baca[0] == 'i')&&(baca[1] == 'n')&&(baca[2] == 'p')&&(baca[3] == 'u')&&(baca[4] == 't'))
                    {
                        if(baca[5] == '(')
                        {
                            a ='(';
                            Push(&S,a);
                            y = 6;
                            c = 1;
                            while(c != 0) {
                                if (baca[y] != '\0') {
                                    if ((baca[y] == ')') && (baca[y+1] == '\0')) {
                                        if (InfoTop(S) == '(') {
                                            Pop(&S,&a);
                                            c = 0;
                                        }
                                    } else if (baca[y] == ')') {
                                        printf("Syntax error\n");
                                        cek = 0;
                                        c = 0;
                                    } else {
                                        y++;
                                    }
                                } else {
                                    if (InfoTop(S) != '(') {
                                        c = 0;
                                    } else {
                                        printf("Syntax error\n");
                                        c = 0;
                                        cek = 0;
                                    }
                                }
                            }
                        }
                        else {
                            printf("syntax error 2\n");
                            cek = 0;
                        }
                    }
                   else if((baca[0] == 'o')&&(baca[1] == 'u')&&(baca[2] == 't')&&(baca[3] == 'p')&&(baca[4] == 'u') &&(baca[5] == 't'))
                    {
                        if(baca[6] == '(')
                        {
                            a ='(';
                            Push(&S,a);
                            y = 7;
                            c = 1;
                            while(c != 0) {
                                if (baca[y] != '\0') {
                                    if ((baca[y] == ')') && (baca[y+1] == '\0')) {
                                        if (InfoTop(S) == '(') {
                                            Pop(&S,&a);
                                            c = 0;
                                        }
                                    } else if (baca[y] == ')') {
                                        printf("Syntax error\n");
                                        cek = 0;
                                        c = 0;
                                    } else {
                                        y++;
                                    }
                                } else {
                                    if (InfoTop(S) != '(') {
                                        c = 0;
                                    } else {
                                        printf("Syntax error\n");
                                        c = 0;
                                        cek = 0;
                                    }
                                }
                            }
                        }
                        else {
                            printf("syntax error 2\n");
                            cek = 0;
                        }
                    }
                }
            }
    }while ((byte_read != 0) && (cek != 0));
    //if(InfoTop(S) == 'b') {;
    //    printf("Program doesn't end correctly\n");
    //} else if(InfoTop(S) == 'w') {
    //    printf("expected 'do' after 'while'\n");
    //}
    //printf("Program compiled\n");
    Pop(&S,&a);
    if ((IsEmpty(S)) &&(cek == 1)) {
        printf("Compiled Successfully\n");
    } else {
        printf("Compiled Unsuccessfully\n");
    }
    return 0;
}

*/
